////#include "DictionaryManager.h"
////#include "../file_manager.h"
////#include "../page.h"
////#include <fstream>
////#include <direct.h>
////#include <io.h>
////#include <cstring>
////
////using namespace std;
////using namespace dbms::managers;
////using namespace dbms::entities;
////
////namespace dbms {
////    namespace managers {
////
////        // 静态成员初始化
////        DictionaryManager* DictionaryManager::instance = nullptr;
////        string DictionaryManager::currentSchema;
////        map<int, pair<RoidType, string>> DictionaryManager::roidMap;
////
////        DictionaryManager* DictionaryManager::getInstance() {
////            if (instance == nullptr) {
////                instance = new DictionaryManager();
////                instance->initialize();
////            }
////            return instance;
////        }
////
////        void DictionaryManager::release() {
////            if (instance != nullptr) {
////                delete instance;
////                instance = nullptr;
////            }
////            storeMapping();
////        }
////
////        void DictionaryManager::initialize() {
////            _mkdir("data");
////            int isExist = _access("./data/dict_oid.oid", 0);
////            if (isExist == -1) {
////                initializeDictionary();
////                User* user = createUser();
////                user->setUserId(deliverOid());
////                user->setUsername("admin");
////                user->setPassword("123456");
////                storeUser(user);
////            }
////
////            initializeMapping();
////            OidManager::getInstance(); // 初始化OID管理器
////
////            // 获取各种文件第一页的页号
////            ifstream fin("./data/firstblockid.bid", ios::in);
////            if (fin.is_open()) {
////                getline(fin, userBlock1);
////                getline(fin, databaseBlock1);
////                getline(fin, classBlock1);
////                getline(fin, attributeBlock1);
////                getline(fin, indexBlock1);
////                fin.close();
////            }
////        }
////
////        void DictionaryManager::initializeDictionary() {
////            FileManager* fileManager = FileManager::getInstance();
////            _mkdir("data");
////
////            // 创建OID文件
////            ofstream fout("./data/dict_oid.oid", ios::out);
////            fout << 1;
////            fout.close();
////
////            // 分配文件
////            file fileUser = *fileManager->getFile(fileManager->allocFile());
////            file fileDatabase = *fileManager->getFile(fileManager->allocFile());
////            file fileClass = *fileManager->getFile(fileManager->allocFile());
////            file fileAttribute = *fileManager->getFile(fileManager->allocFile());
////            file fileIndex = *fileManager->getFile(fileManager->allocFile());
////
////            // 为各种实体类型创建数据文件
////            vector<file> files = { fileUser, fileDatabase, fileClass, fileAttribute, fileIndex };
////
////            for (const auto& f : files) {
////                string filePath = "./data/data_" + f.fileid64 + ".db";
////                ofstream dataFile(filePath, ios::out);
////                dataFile.close();
////
////                string blockId = FileManager::getInstance()->allocPage(f.fileid64);
////
////                ofstream blockFile("./data/firstblockid.bid", ios::out | ios::app);
////                blockFile << blockId << "\n";
////                blockFile.close();
////            }
////        }
////
////        void DictionaryManager::initializeMapping() {
////            ifstream fin("./data/dict.roid", ios::in);
////            if (!fin.is_open()) {
////                return; // 文件不存在，返回
////            }
////
////            string line;
////            while (getline(fin, line)) {
////                if (line.empty() || line.length() < 3) {
////                    continue; // 跳过空行或太短的行
////                }
////
////                try {
////                    Stat* stat = Page::stringToStat(line);
////                    if (stat && stat->statList && stat->statList->size() >= 3) {
////                        int oid = (*(stat->statList))[0]->ival;
////                        RoidType type = (RoidType)(*(stat->statList))[1]->ival;
////                        string rowId = (*(stat->statList))[2]->name;
////                        roidMap[oid] = make_pair(type, rowId);
////                    }
////                }
////                catch (...) {
////                    // 忽略解析错误，继续
////                    continue;
////                }
////            }
////            fin.close();
////        }
////
////        void DictionaryManager::storeMapping() {
////            ofstream fout("./data/dict.roid", ios::out);
////            if (!fout.is_open()) {
////                return;
////            }
////
////            for (const auto& item : roidMap) {
////                vector<Stat*> stats;
////                Stat* oidStat = Stat::makeLiteral((int64_t)item.first);
////                Stat* roidTypeStat = Stat::makeLiteral((int64_t)item.second.first);
////                Stat* ridStat = Stat::makeLiteral(item.second.second.c_str());
////
////                stats.push_back(oidStat);
////                stats.push_back(roidTypeStat);
////                stats.push_back(ridStat);
////
////                Stat* resStat = Stat::makeArray(&stats);
////                string resStr = Page::statToString(resStat);
////                fout << resStr << endl;
////            }
////            fout.close();
////        }
////
////        int DictionaryManager::deliverOid() {
////            return OidManager::getInstance()->getNextOid();
////        }
////
////        // 实体创建相关方法
////        User* DictionaryManager::createUser() {
////            return new User();
////        }
////
////        Database* DictionaryManager::createDatabase() {
////            return new Database();
////        }
////
////        Class* DictionaryManager::createClass() {
////            return new Class();
////        }
////
////        Attribute* DictionaryManager::createAttribute() {
////            return new Attribute();
////        }
////
////        Index* DictionaryManager::createIndex() {
////            return new Index();
////        }
////
////        // 查询方法实现
////        User* DictionaryManager::getUser(const string& username) {
////            User* targetUser = createUser();
////            vector<string> rowidList;
////
////            for (const auto& item : roidMap) {
////                if (item.second.first == UserRoid) {
////                    rowidList.push_back(item.second.second);
////                }
////            }
////
////            vector<Stat*> tempStats = FileManager::getInstance()->multipleGet(rowidList);
////
////            for (Stat* stat : tempStats) {
////                if (stat && stat->statList && stat->statList->size() >= 3) {
////                    string tempUsername = (*(stat->statList))[1]->name;
////                    if (tempUsername == username) {
////                        targetUser->setUserId((*(stat->statList))[0]->ival);
////                        targetUser->setUsername(username);
////                        targetUser->setPassword((*(stat->statList))[2]->name);
////                        return targetUser;
////                    }
////                }
////            }
////
////            delete targetUser;
////            return nullptr;
////        }
////
////        Database* DictionaryManager::getDatabase(User* user, const string& dbname) {
////            if (!user) return nullptr;
////
////            Database* targetDatabase = createDatabase();
////            int userId = user->getUserId();
////            vector<string> rowidList;
////
////            for (const auto& item : roidMap) {
////                if (item.second.first == DatabaseRoid) {
////                    rowidList.push_back(item.second.second);
////                }
////            }
////
////            vector<Stat*> tempStats = FileManager::getInstance()->multipleGet(rowidList);
////
////            for (Stat* stat : tempStats) {
////                if (stat && stat->statList && stat->statList->size() >= 6) {
////                    int tempUserId = (*(stat->statList))[2]->ival;
////                    string tempDbName = (*(stat->statList))[1]->name;
////
////                    if (tempDbName == dbname && tempUserId == userId) {
////                        targetDatabase->setOid((*(stat->statList))[0]->ival);
////                        targetDatabase->setDatabaseName(dbname);
////                        targetDatabase->setOwnerId(tempUserId);
////                        targetDatabase->setConnectionLimit((*(stat->statList))[3]->ival);
////
////                        // 设置访问控制列表
////                        if ((*(stat->statList))[5]->statList) {
////                            vector<int> acl;
////                            for (Stat* aclStat : *(*(stat->statList))[5]->statList) {
////                                acl.push_back(aclStat->ival);
////                            }
////                            targetDatabase->setAccessControlList(acl);
////                        }
////
////                        return targetDatabase;
////                    }
////                }
////            }
////
////            delete targetDatabase;
////            return nullptr;
////        }
////
////        vector<Database*> DictionaryManager::getDatabases(User* user) {
////            vector<Database*> result;
////            if (!user) return result;
////
////            int userId = user->getUserId();
////            vector<string> rowidList;
////
////            for (const auto& item : roidMap) {
////                if (item.second.first == DatabaseRoid) {
////                    rowidList.push_back(item.second.second);
////                }
////            }
////
////            vector<Stat*> tempStats = FileManager::getInstance()->multipleGet(rowidList);
////
////            for (Stat* stat : tempStats) {
////                if (stat && stat->statList && stat->statList->size() >= 6) {
////                    int tempUserId = (*(stat->statList))[2]->ival;
////
////                    if (tempUserId == userId) {
////                        Database* database = createDatabase();
////                        database->setOid((*(stat->statList))[0]->ival);
////                        database->setDatabaseName((*(stat->statList))[1]->name);
////                        database->setOwnerId(tempUserId);
////                        database->setConnectionLimit((*(stat->statList))[3]->ival);
////
////                        // 设置访问控制列表
////                        if ((*(stat->statList))[5]->statList) {
////                            vector<int> acl;
////                            for (Stat* aclStat : *(*(stat->statList))[5]->statList) {
////                                acl.push_back(aclStat->ival);
////                            }
////                            database->setAccessControlList(acl);
////                        }
////
////                        result.push_back(database);
////                    }
////                }
////            }
////
////            return result;
////        }
////
////        // 存储方法实现
////        void DictionaryManager::storeUser(User* user) {
////            if (user) {
////                user->storeToFile();
////                roidMap[user->getUserId()] = make_pair(UserRoid, ""); // 需要获取实际的rowId
////            }
////        }
////
////        void DictionaryManager::storeDatabase(Database* database) {
////            if (database) {
////                database->storeToFile();
////                roidMap[database->getOid()] = make_pair(DatabaseRoid, ""); // 需要获取实际的rowId
////            }
////        }
////
////        void DictionaryManager::storeClass(Class* cls) {
////            if (cls) {
////                cls->storeToFile();
////                roidMap[cls->getOid()] = make_pair(ClassRoid, ""); // 需要获取实际的rowId
////            }
////        }
////
////        void DictionaryManager::storeAttribute(Attribute* attribute) {
////            if (attribute) {
////                attribute->storeToFile();
////                roidMap[attribute->getOid()] = make_pair(AttributeRoid, ""); // 需要获取实际的rowId
////            }
////        }
////
////        void DictionaryManager::storeIndex(Index* index) {
////            if (index) {
////                index->storeToFile();
////            }
////        }
////
////        // 更新方法实现
////        void DictionaryManager::updateUser(User* user) {
////            if (user) {
////                user->updateFile();
////            }
////        }
////
////        void DictionaryManager::updateDatabase(Database* database) {
////            if (database) {
////                database->updateFile();
////            }
////        }
////
////        void DictionaryManager::updateClass(Class* cls) {
////            if (cls) {
////                cls->updateFile();
////            }
////        }
////
////        void DictionaryManager::updateAttribute(Attribute* attribute) {
////            if (attribute) {
////                attribute->updateFile();
////            }
////        }
////
////        // 辅助方法实现
////        string DictionaryManager::getRowId(int oid) {
////            auto it = roidMap.find(oid);
////            if (it != roidMap.end()) {
////                return it->second.second;
////            }
////            return "";
////        }
////
////        Attribute* DictionaryManager::findColumn(Class* cls, const vector<Attribute*>& attrs, const char* colName) {
////            for (Attribute* attr : attrs) {
////                if (attr && _stricmp(attr->getAttributeName().c_str(), colName) == 0) {
////                    return attr;
////                }
////            }
////            return nullptr;
////        }
////
////        string DictionaryManager::columnNotExistError(const char* colName, int line, int col) {
////            char buffer[256];
////            snprintf(buffer, sizeof(buffer),
////                "Error: column '%s' does not exist (line %d, column %d)",
////                colName, line, col);
////            return string(buffer);
////        }
////
////        // 其他查询方法的文件实现，需要根据具体需求设计：
////        Class* DictionaryManager::getClass(Database* database, const string& relationName) {
////            // 待实现，需要根据具体需求设计
////            return nullptr;
////        }
////
////        vector<Class*> DictionaryManager::getClasses(Database* database) {
//            // 待实现，需要根据具体需求设计
//            return vector<Class*>();
//        }
//
//        vector<Attribute*> DictionaryManager::getAttributes(Class* table) {
//            // 待实现，需要根据具体需求设计
//            return vector<Attribute*>();
//        }
//
//        Attribute* DictionaryManager::getAttribute(int attributeId) {
//            // 待实现，需要根据具体需求设计
//            return nullptr;
//        }
//
//        vector<Attribute*> DictionaryManager::getAttributes(Index* index) {
//            // 待实现，需要根据具体需求设计
//            return vector<Attribute*>();
//        }
//
//        Index* DictionaryManager::getIndex(Class* table) {
//            // 待实现，需要根据具体需求设计
//            return nullptr;
//        }
////
////    } // namespace managers
//////} // namespace dbms